<?php

    /**
     *      [Discuz!] (C)2001-2099 Comsenz Inc.
     *      This is NOT a freeware, use is subject to license terms
     *      $Id: table_mobile_setting.php 31281 2012-08-03 02:29:27Z zhangjie $
     */

    if (!defined('IN_DISCUZ')) {
        exit('Access Denied');
    }

    class table_cloud_orders extends discuz_table {

        public function __construct() {
            $this->_table = 'cloud_orders';
            $this->_pk = 'coid';

            parent::__construct();
        }

        //插入数据库
        public function insert($arr) {
            return DB::insert($this->_table, $arr, true);
        }

        //更新数据库
        public function update($id, $data) {
            $id = intval($id);
            if (!$data) {
                return;
            }
            return DB::update($this->_table, $data, DB::field($this->_pk, $id));
        }

        //查找某条记录
        public function fetch_info($id) {
            $id = intval($id);
            return DB::fetch_first("SELECT * FROM %t WHERE ".$this->_pk."=%d", array($this->_table, $id));
        }
        //根据keys称查询
        public function fetch_product_by_name($cadid ,$start, $limit) {
            return DB::fetch_all('SELECT * FROM '.$this->_table.' WHERE cadid = '.$cadid . DB::limit($start, $limit));
        }

        //根据keys称查询
        public function fetch_orders_by_ordersNo($ordersNo) {
            return DB::fetch_first('SELECT * FROM '.DB::table($this->_table).' WHERE ordersNo = \''.$ordersNo .'\'');
        }

        //查询所有limt数据
        public function fetch_all_limit($cadid, $start = 0, $limit = 0,$param=0) {
            if(empty($param)){
                $where = " ";
            }else{
                $where = " and status='".$param."'";
            }
            return DB::fetch_all('SELECT * FROM ' . DB::table($this->_table) . ' where cadid=' . $cadid .$where.' order by '.DB::order('paymentstatus','ASC'). DB::limit($start, $limit));
        }
        //查询count
        public function fetch_all_count($cadid,$param=0) {
            if(empty($param)){
                $where = " ";
            }else{
                $where = " and status='".$param."'";
            }
            return DB::fetch_first('SELECT count(coid) as count FROM ' . DB::table($this->_table) . ' where cadid='. $cadid. $where);
        }

        //查询所有limt数据
        public function fetch_all_where($cuid,$cadid, $start = 0, $limit = 0) {
            return DB::fetch_all('SELECT * FROM ' . DB::table($this->_table) . ' where cuid='.$cuid.' and  cadid=' . $cadid .' order by '.DB::order('lasttime','desc'). DB::limit($start, $limit));
        }

        // 删除
        public function delete_by_id($id) {
            return DB::query("DELETE FROM %t WHERE ".$this->_pk."=%d", array($this->_table, $id));
        }
        //得到未完成的订单
        public function fetch_all_not_finish_limit($cuid,$cadid, $start = 0, $limit = 0){
            return DB::fetch_all("SELECT * FROM " . DB::table($this->_table) . " where cuid=".$cuid." and  cadid=" . $cadid ." and status not in('已完成') order by ".DB::order('lasttime','desc'). DB::limit($start, $limit));
        }

        //得到已完成的订单
        public function fetch_all_finish_limit($cuid,$cadid, $start = 0, $limit = 0){
            return DB::fetch_all("SELECT * FROM " . DB::table($this->_table) . " where cuid=".$cuid." and  cadid=" . $cadid ." and status in('已完成') order by ".DB::order('lasttime','desc'). DB::limit($start, $limit));
        }


       public function fetch_all_limit_search($cadid,$keyword, $start = 0, $limit = 0){
           return DB::fetch_all('SELECT * FROM ' . DB::table($this->_table) . ' where cadid=' . $cadid .' and (
           orderer like \'%'.$keyword.'%\' or otel like \'%'.$keyword.'%\' or ordersNo like \'%'.$keyword.'%\' ) order by '.DB::order('lasttime','desc'). DB::limit($start, $limit));

       }
        public function fetch_all_search($cadid,$keyword, $start = 0, $limit = 0,$param=0){
            if(empty($param)){
                $where = " ";
            }else{
                $where = " and status='".$param."' ";
            }
            return DB::fetch_all('SELECT * FROM ' . DB::table($this->_table) . ' where cadid=' . $cadid .$where.' and (
           orderer like \'%'.$keyword.'%\' or otel like \'%'.$keyword.'%\' or ordersNo like \'%'.$keyword.'%\' ) order by '.DB::order('lasttime','desc'));

        }

        public function fetch_all_limit_search_count($cadid,$keyword){
            return DB::fetch_first('SELECT count(coid) as count FROM ' . DB::table($this->_table) . ' where cadid=' . $cadid .' and ( orderer like \'%'.$keyword.'%\' or otel like \'%'.$keyword.'%\' or ordersNo like \'%'.$keyword.'%\' )');
        }

    }